package com.jieshuibao.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jieshuibao.mapper.UserDealRecordMapper;
import com.jieshuibao.mapper.UserDramaMapper;
import com.jieshuibao.mapper.UserMapper;
import com.jieshuibao.model.db.User;
import com.jieshuibao.model.db.UserDealRecord;
import com.jieshuibao.model.db.UserDrama;
import com.jieshuibao.model.db.UserDramaExample;
import com.jieshuibao.util.PayTypeUtil;
import com.jieshuibao.util.UUIDGenerator;
import com.jieshuibao.wx.util.PayCommonUtil;

@Service
public class UserDramaService{
	
	@Resource
	private UserDramaMapper userDramaMapper;
	@Resource
	private UserDealRecordMapper userDealRecordMapper;
	@Resource
	private UserMapper userMapper;
	
	public void addUserDrama(UserDrama userDrama) {
		userDramaMapper.insertSelective(userDrama);
	}
	
	public void delUserDramaById(String id) {
		userDramaMapper.deleteByPrimaryKey(id);
	}
	
	public void updateUserDrama(UserDrama userDrama) {
		userDramaMapper.updateByPrimaryKeySelective(userDrama);
	}
	
	public void updateUserDramaByExample(UserDrama userDrama,UserDramaExample userDramaExample){
	    userDramaMapper.updateByExampleSelective(userDrama,userDramaExample);
	}
	
	public UserDrama getUserDramaById(String id) {
		return userDramaMapper.selectByPrimaryKey(id);
	}
	
	public List<UserDrama> getPageUserDrama(UserDramaExample userDramaExample) {
		return userDramaMapper.selectPage(userDramaExample);
	}
	
	public int countUserDrama(UserDramaExample userDramaExample) {
		return userDramaMapper.countByExample(userDramaExample);
	}
	
	public List<UserDrama> selectByExample(UserDramaExample example){
	    return userDramaMapper.selectByExample(example);
	}
	public void addUserDramaAndUpdateUser(User user,UserDrama userDrama){
		if(userDrama!=null&&user!=null){
			String uuid1 = UUIDGenerator.getUUID();//用户支出流水
			String uuid2 = UUIDGenerator.getUUID();//解税宝收入流水
			UserDealRecord userDealRecord1=new UserDealRecord();
			UserDealRecord userDealRecord2=new UserDealRecord();
			userDealRecord1.setUuid(uuid1);
			userDealRecord2.setUuid(uuid2);
			userDealRecord1.setUserId(user.getPhoneNumber());
			userDealRecord1.setAccountType(PayTypeUtil.ACCOUNTTYPE_PERSONAL);
			userDealRecord2.setUserId(PayTypeUtil.COMPANY_ACCPUNT);
			userDealRecord2.setAccountType(PayTypeUtil.ACCOUNTTYPE_BUSINESS);
			String id = userDrama.getId();
			
			userDealRecord1.setBusinessNo(id );
			userDealRecord2.setBusinessNo(id );
			Date date = new Date();
			userDealRecord1.setCreateDate(date);
			userDealRecord2.setCreateDate(date);
			Double money = userDrama.getMoney();
			
			userDealRecord1.setSum(money);
			userDealRecord2.setSum(money);
			
			userDealRecord1.setGoodsType(PayTypeUtil.GOODSTYPE_VIDEOS);
			userDealRecord2.setGoodsType(PayTypeUtil.GOODSTYPE_VIDEOS);
			userDealRecord1.setTradeType(PayTypeUtil.TRDETYPE_EXPENDITURE);
			userDealRecord2.setTradeType(PayTypeUtil.TRDETYPE_INCOME);
			userDealRecord1.setPaySucTime(date);
			userDealRecord2.setPaySucTime(date);
			userDealRecord1.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
			userDealRecord2.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
			userDealRecord1.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
			userDealRecord2.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
			userMapper.updateByPrimaryKeySelective(user);
			userDramaMapper.insertSelective(userDrama);
			userDealRecordMapper.insertSelective(userDealRecord1);
			userDealRecordMapper.insertSelective(userDealRecord2);
		}
		
		
		
	}
	public void updateAfterBuy(UserDrama userDramaById,
			UserDealRecord userDealRecord, UserDealRecord userDealRecord1) {
		userDramaMapper.updateByPrimaryKeySelective(userDramaById);
		userDealRecordMapper.insert(userDealRecord);
		userDealRecordMapper.insert(userDealRecord1);
	}
}
